package com.zc.redisstream.listener;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zc.domain.PhoenixAlarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhangcheng
 * @date 2022/9/5 21:57
 * @description
 */

@Component
public class PhoenixAlarmStreamListener implements StreamListener<String, MapRecord<String, String, String>> {
    private final Logger logger = LoggerFactory.getLogger(PhoenixAlarmStreamListener.class);

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        try {
            String stream = message.getStream();
            RecordId messageId = message.getId();
            Map<String, String> valueMap = message.getValue();
            logger.info("系统消息处理: streamKey: {}, message id: {}, valueMap: {}",stream,messageId,valueMap);
            PhoenixAlarm jsonObject = JSON.parseObject(valueMap.get("phoenixAlarm"), PhoenixAlarm.class);

        } catch (Exception e) {
            logger.error("系统告警处理异常",e);
        }finally {
            redisTemplate.opsForStream().delete(message);
        }
    }
}
